package com.hyk.commonLib.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import androidx.annotation.ColorInt;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.hyk.commonLib.R;
import com.hyk.commonLib.common.utils.ScreenUtils;

/**
 * Created by 贺玉琨 on 2020/5/4.
 */

public class RoundCornerConstraintLayout extends ConstraintLayout {
    private float[] rect = new float[]{0, 0, 0, 0, 0, 0, 0, 0};
    private Path path;
    @ColorInt
    private int backColor;

    public RoundCornerConstraintLayout(Context context) {
        super(context);
    }

    public RoundCornerConstraintLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundCornerConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initBackground(attrs);
    }

    protected void initBackground(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RoundCornerConstraintLayout);
        backColor = a.getColor(R.styleable.RoundCornerConstraintLayout_backColor, Color.TRANSPARENT);
        float cornerRadius = a.getDimension(R.styleable.RoundCornerConstraintLayout_cornerRadius, ScreenUtils.dp2px(5, this));
        float cornerRadiusLT = a.getDimension(R.styleable.RoundCornerConstraintLayout_cornerRadiusLT, cornerRadius);
        float cornerRadiusRT = a.getDimension(R.styleable.RoundCornerConstraintLayout_cornerRadiusRT, cornerRadius);
        float cornerRadiusLB = a.getDimension(R.styleable.RoundCornerConstraintLayout_cornerRadiusLB, cornerRadius);
        float cornerRadiusRB = a.getDimension(R.styleable.RoundCornerConstraintLayout_cornerRadiusRB, cornerRadius);
        a.recycle();
        setRound(cornerRadiusLT, cornerRadiusRT, cornerRadiusRB, cornerRadiusLB, false);
    }

    public void setRound(float radius) {
        setRound(radius, radius, radius, radius, true);
    }

    public void setRound(float radius, boolean inDp) {
        setRound(radius, radius, radius, radius, inDp);
    }

    public void setRound(float leftTop, float rightTop, float rightBottom, float leftBottom) {
        setRound(leftTop, rightTop, rightBottom, leftBottom, true);
    }

    public void setRound(float leftTop, float rightTop, float rightBottom, float leftBottom, boolean inDp) {
        if (inDp) {
            leftTop = ScreenUtils.dp2px(leftTop);
            rightTop = ScreenUtils.dp2px(rightTop);
            rightBottom = ScreenUtils.dp2px(rightBottom);
            leftBottom = ScreenUtils.dp2px(leftBottom);
        }
        this.rect = new float[]{leftTop, leftTop, rightTop, rightTop, rightBottom, rightBottom, leftBottom, leftBottom};
        this.path = null;
    }

    public void setLtRound(float cornerRadius) {
        setLtRound(cornerRadius, true);
    }

    public void setRtRound(float cornerRadius) {
        setRtRound(cornerRadius, true);
    }

    public void setRbRound(float cornerRadius) {
        setRbRound(cornerRadius, true);
    }

    public void setLbRound(float cornerRadius) {
        setLbRound(cornerRadius, true);
    }

    public void setLtRound(float cornerRadius, boolean inDp) {
        cornerRadius = inDp ? ScreenUtils.dp2px(cornerRadius) : cornerRadius;
        this.rect[0] = cornerRadius;
        this.rect[1] = cornerRadius;
        this.path = null;
    }

    public void setRtRound(float cornerRadius, boolean inDp) {
        cornerRadius = inDp ? ScreenUtils.dp2px(cornerRadius) : cornerRadius;
        this.rect[2] = cornerRadius;
        this.rect[3] = cornerRadius;
        this.path = null;
    }

    public void setRbRound(float cornerRadius, boolean inDp) {
        cornerRadius = inDp ? ScreenUtils.dp2px(cornerRadius) : cornerRadius;
        this.rect[4] = cornerRadius;
        this.rect[5] = cornerRadius;
        this.path = null;
    }

    public void setLbRound(float cornerRadius, boolean inDp) {
        cornerRadius = inDp ? ScreenUtils.dp2px(cornerRadius) : cornerRadius;
        this.rect[6] = cornerRadius;
        this.rect[7] = cornerRadius;
        this.path = null;
    }

    @Override
    public void dispatchDraw(Canvas canvas) {
//        if (isInEditMode()) {
//            super.dispatchDraw(canvas);
//            return;
//        }
        if (this.rect != null) {
            if (this.path == null) {
                int width = this.getWidth();
                int height = this.getHeight();
                this.path = new Path();
                RectF bound = new RectF(0.0F, 0.0F, (float) width, (float) height);
                this.path.addRoundRect(bound, this.rect, Path.Direction.CW);
            }
            canvas.clipPath(this.path);
            canvas.drawColor(backColor);
        }
        super.dispatchDraw(canvas);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        path = null;
    }
}
